package com.itboye.sunsun.personsetting.ui;

import android.content.Intent;
import android.os.Bundle;
import android.test.PerformanceTestCase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class SetEmailActivity extends BaseActivity implements OnClickListener {

	private EditText edittext;
	private Button confirm;
	private ImageView back;
	public static String EMAILACTION = "com.itboye.nick";

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_email);
		setStatusBarColor(R.color.home_blue);
		String uri=(String) SPUtils.get(App.ctx,null,SPContants.EMAIL,"");
		edittext.setHint(uri);
	}

	
	@Override
	public void onClick(View v) {

		if (v.getId() == R.id.confirm) {

			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(edittext.getText().toString());
			if (matcher.matches()
					&& !edittext.getText().toString().equals(null)) {
				setEmail();
			} else {
				Toast.makeText(App.ctx, "邮箱输入错误", 0).show();
			}
		} else if (v.getId() == R.id.back) {
			this.finish();
		}

	}

	public void setEmail() {

		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid",
						(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
								""))
				.param("email", edittext.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						// Toast.makeText(App.ctx, "修改成功", 0).show();

						SPUtils.put(App.ctx, null, SPContants.EMAIL, edittext
								.getText().toString());

						Intent intent = new Intent(SetEmailActivity.this,
								PersonSettingActivity.class);
						intent.putExtra("email", edittext.getText().toString());

						setResult(RESULT_OK, intent);

						SetEmailActivity.this.finish();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

}
